#!/bin/bash

RUBIC=rubic
RUBY=ruby
LLVM=llvm
TMP=tmp
BIN=bin
VAL=valeurs

make
echo
rm -rf $TMP $BIN $LLVM $RUBY/*~ $VAL/*~

mkdir $TMP
mkdir $BIN
mkdir $LLVM

declare -a TAB='()'
i=0
for file in `ls -R ./$RUBY/`
do
    TAB[$i]=$file
    ((i++))
done

for ((i = 0; i<${#TAB[*]} ; i += 1))
do
    TAB[$i]=$(echo "${TAB[$i]}" | cut -f1 -d '.')
done

for ((i = 1; i<${#TAB[*]} ; i += 1))
do
    ./$RUBIC < $RUBY/${TAB[$i]}.rb > $LLVM/${TAB[$i]}.ll
    llvm-as $LLVM/${TAB[$i]}.ll -f -o $TMP/${TAB[$i]}.bc
    llc $TMP/${TAB[$i]}.bc -o $TMP/${TAB[$i]}.s
    gcc -Wall -Wextra -O2 $TMP/${TAB[$i]}.s print.o -o $BIN/${TAB[$i]}
    echo -n "${TAB[$i]} : "
    ./$BIN/${TAB[$i]}
    echo -n "          Valeur attendue : "
    cat $VAL/${TAB[$i]}.txt
    echo
done

rm -rf $TMP
